Examples of `{{ }}`
Let’s explore some common use cases for {{ }} in YAML with examples:
Example 1: Tagging Scalars
age: !!int 20
In this example, we have a YAML key-value pair where the key is “age,” and the value is `!!int 20`.
- `!!int` inside `{{ }}` is a YAML tag that tells the YAML parser to interpret the value as an integer.
- The `20` is a scalar value (a plain number) without any quotes or special notation.
When this YAML document is parsed, the YAML parser will recognize that the `age` value is tagged as an integer (`!!int`), ensuring that it’s treated as a numeric data type rather than a string. This can be useful when you want to maintain the data type’s integrity.
Example 2: Applying Transformations
name: !!str {{ name.toUpperCase() }}
In this example, we have a YAML key-value pair where the key is “name,” and the value is `{{ name.toUpperCase() }}`.
- `{{ }}` is used to apply a transformation to the value of `name`.
- name.toUpperCase() is a JavaScript-like expression enclosed within `{{ }}`. It transforms the `name` variable to uppercase.
When the YAML document is processed, it will evaluate the expression `name.toUpperCase()` and assign the result (the uppercase version of the `name` variable) as the value for the “name” key.
This is a powerful way to modify or preprocess data within YAML files, especially when dealing with configuration files or data transformations.
Example 3: Environment Variable Substitution
database_url: {{ env('DB_URL') }}
In this example, we have a YAML key-value pair where the key is “database_url,” and the value is`{{ env(‘DB_URL’) }}`.
- `{{ env(‘DB_URL’) }}` is a common use case for `{{ }}` in configuration files. It’s used to substitute the value with the contents of the `DB_URL` environment variable.
When the YAML document is processed, the `{{ env(‘DB_URL’) }}` expression is replaced with the actual value of the `DB_URL` environment variable, effectively setting the “database_url” key to the database URL stored in the environment variable.
This is a valuable feature for injecting dynamic or sensitive data into configuration files without hardcoding it, making the configuration more flexible and secure.
Nested `{{ }}`
value: !!int {{ env('SOME_ENV_VARIABLE').toUpperCase() }}
In this example, we have a YAML key-value pair where the key is “value,” and the value is a combination of nested {{ }} constructs.
- `{{ env(‘SOME_ENV_VARIABLE’) }}` retrieves the value of the “SOME_ENV_VARIABLE” environment variable.
- `.toUpperCase()` is applied to the result of `{{ env(‘SOME_ENV_VARIABLE’) }}` to convert it to uppercase.
- Finally, `!!int` tags the entire expression as an integer.
When processed, this YAML document will:
- Retrieve the value of the “SOME_ENV_VARIABLE” environment variable.
- Convert it to uppercase.
- Treat the result as an integer.
The resulting integer value is then assigned to the “value” key.
This example illustrates the flexibility of `{{ }}` in allowing you to perform multiple operations on a value within a YAML document.
What does {{ }} mean in YAML?
YAML (YAML Ain’t Markup Language) is a popular human-readable data serialization format often used for configuration files and data exchange. YAML’s simplicity and readability have made it a favorite among developers and system administrators. While YAML is generally straightforward, there are times when you encounter unfamiliar constructs, such as `{{ }}`. In this article, we’ll delve into what `{{ }}` means in YAML, provide examples, and cover every aspect of its usage.
Contact Us